let requestUrl = null;

function addHandler() {
	requestUrl = "/chengwei/insert";

	$("#chengwei_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#chengwei_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/chengwei/updateById";

		$("#id").val(rowData.id);
		$("#get").textbox("setValue", rowData.get);
		$("#note").textbox("setValue", rowData.note);
		$("#name").textbox("setValue", rowData.name);
		$("#type").combobox("setValue", rowData.type);
		$("#permanent").combobox("setValue", rowData.permanent);

		$("#chengwei_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

/**
 * 批量删除
 */
function deleteHandler() {
	let rows = $("#chengwei_list").datagrid("getChecked");
	let count = rows.length;

	if (count > 0) {
		confirmAction("确认删除勾选的" + count + "条数据？", function() {
			let ids = [];

			for (let i = 0; i < rows.length; i++) {
				let id = rows[i].id;

				ids.push(id);
			}

			/**
			 * 批量删除
			 */
			ajaxPost("/chengwei/deleteByIds", {
				ids: JSON.stringify(ids)
			}, function(resp) {
				showMsg(resp.message);

				$("#chengwei_list").datagrid("reload");
				$("#chengwei_list").datagrid("uncheckAll");
				$("#chengwei_list").datagrid("unselectAll");
			}, error);
		});
	} else {
		alertMsg("请选择要删除的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#name").textbox({
		width: 120,
		required: true
	});

	fetchSelect("#type", "/chengwei_type/selectByType?type=1", {
		width: 120,
		required: true
	});
	fetchSelect("#permanent", "/chengwei_type/selectByType?type=2", {
		width: 120,
		required: true
	});

	$("#get").textbox({
		width: 320,
		required: true
	});

	$("#note").textbox({
		width: 320,
		height: 80,
		required: true,
		multiline: true
	});
	
	$("#chengwei_dialog").dialog({
		title: "称谓信息",
		width: 440,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#chengwei_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#chengwei_dialog").dialog("close");
						$("#chengwei_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#chengwei_form").form("clear");
				$("#chengwei_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr * 15;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_filter + heigt_body + heigt_footer + heigt_scoll_bar + 2;
	/**
	 * 可选页码
	 * @type {number[]}
	 */
	let pageList = [15, 30, 100, 200, 500]

	let datagrid = $("#chengwei_list").datagrid({
		url: "/chengwei/selectByPage",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		pageList: pageList,
		pageSize: pageList[0],
		remoteFilter: true,
		clientPaging: false,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}],
		columns: [[
			{field: "ck", checkbox: true},
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "name", title: "称谓名称", align: "center", width: 80},
			{field: "type", title: "称谓类型", align: "center", width: 80
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/chengwei_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#type_" + rowIndex).html(data.name);
					}, error);

					return "<div id='type_" + rowIndex + "'></div>";
				}
			},
			{field: "permanent", title: "是否限时", align: "center", width: 80
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/chengwei_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#permanent_" + rowIndex).html(data.name);
					}, error);

					return "<div id='permanent_" + rowIndex + "'></div>";
				}
			},
			{field: "get", title: "称谓获得方式", align: "center", width: 200
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			{field: "note", title: "称谓信息", align: "center", width: 200
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			}
		]]
	});

	datagrid.datagrid("enableFilter", [{
		field: "name",
		type: "textbox",
		operator: ["equal", "contains"]
	}, {
		field: "type",
		type: "combobox",
		options: {
			url: "/chengwei_type/selectByType?type=1",
			valueField: "id",
			textField: "name",
			editable: false,
			panelHeight: "auto"
		},
		operator: ["equal", "notequal"]
	}, {
		field: "permanent",
		type: "combobox",
		options: {
			url: "/chengwei_type/selectByType?type=2",
			valueField: "id",
			textField: "name",
			editable: false,
			panelHeight: "auto"
		},
		operator: ["equal", "notequal"]
	}]);

});